package cn.hznc.configuration;

import cn.hznc.filter.MyInterceptor;
import com.netflix.discovery.converters.Auto;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    /**
     * 跨域访问配置接口.跨域，注释掉，由网关统一处理
     * 自己调试前端时需要先放开，部署的时候要注释掉
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
//                .allowedHeaders("*")
//                .maxAge(3600);
//    }

    @Bean
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/employee/login","/employee/login2","/getToolParam","/GetTool","/GetToolListWithPages"  , "/employee/register" , "assemblyTool/uploadFile" , "/downloadFile" , "/GetNxProgramParams" , "/GetToolReload" , "/GetCutParams" , "/test23");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> list) {
        Iterator<HttpMessageConverter<?>> iterator = list.iterator();
        while (iterator.hasNext()){
            HttpMessageConverter<?> next = iterator.next();
            if(next instanceof StringHttpMessageConverter){
                StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) next;
                stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                break;
            }
        }
    }


}
